import { otherRouter, appRouter } from '@/router/router';
import Util from '@/libs/util';
import Cookies from 'js-cookie';
import Vue from 'vue';
import menuRouter from '@/router/menuRouter'

const app = {
    state: {
        // mobileReg: /^((13[0-9])|(14[5,7,9])|(15[^])|(16[6,9])|(18[0-9])|(19[8,9])|(17[0,1,3,5,6,7,8]))\d{8}$/,
        mobileReg: /^1[3-9]\d{9}$/,
        pwdReg: /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,20}$/,
        domain: 'http://obo5rptyl.bkt.clouddn.com/',
        fault: {}, //首页图表tab数据
        articles: {},
        voucher: {},
        currentPath: [{
            title: '状态总览',
            path: '',
            name: 'home_index'
        }], // 面包屑数组
        currentPageName: '',
        menuList: [],
        routers: [
            otherRouter,
            ...appRouter
        ],
        menuRouter: menuRouter,
        newMenuRouter: [],
        openedSubmenuArr: ['home_index'], // 要展开的菜单数组
    },
    mutations: {
        setFaultData(state, faultdata) {
            state.fault = faultdata;
        },
        setArticlestData(state, articlesdata) {
            state.articles = faultdata;
        },
        setVoucherData(state, voucherdata) {
            state.voucher = faultdata;
        },
        setNewMenuRouter(state, newMenuRouter) {
            state.newMenuRouter = newMenuRouter;
        },

        updateMenulist(state) {
            // let accessCode = parseInt(Cookies.get('access'));
            let menuList = [];
            state.newMenuRouter.forEach((item, index) => {
                // if (item.access !== undefined) {
                //     if (Util.showThisRoute(item.access, accessCode)) {
                //         if (item.children.length === 1) {
                //             menuList.push(item);
                //         } else {
                //             let len = menuList.push(item);
                //             let childrenArr = [];
                //             childrenArr = item.children.filter(child => {
                //                 if (child.access !== undefined) {
                //                     if (child.access === accessCode) {
                //                         return child;
                //                     }
                //                 } else {
                //                     return child;
                //                 }
                //             });
                //             menuList[len - 1].children = childrenArr;
                //         }
                //     }
                // } 
                // else {
                if (item.children.length === 1) {
                    menuList.push(item);
                } else {
                    let len = menuList.push(item);
                    let childrenArr = [];
                    childrenArr = item.children.filter(child => {
                        // if (child.access !== undefined) {
                        //     if (Util.showThisRoute(child.access, accessCode)) {
                        //         return child;
                        //     }
                        // } else {
                        return child;
                        // }
                    });
                    if (childrenArr === undefined || childrenArr.length === 0) {
                        menuList.splice(len - 1, 1);
                    } else {
                        let handledItem = JSON.parse(JSON.stringify(menuList[len - 1]));
                        handledItem.children = childrenArr;
                        menuList.splice(len - 1, 1, handledItem);
                    }
                }
                //}
            });
            state.menuList = menuList;
            localStorage.menuList = JSON.stringify(menuList);
        },
        setCurrentPath(state, pathArr) {
            state.currentPath = pathArr;
        },
        setCurrentPageName(state, name) {
            state.currentPageName = name;
        },
        addOpenSubmenu(state, name) {
            let hasThisName = false
            let isEmpty = false
            if (name.length === 0) {

                isEmpty = true
            }
            if (state.openedSubmenuArr.indexOf(name) > -1) {

                hasThisName = true
            }
            if (!hasThisName && !isEmpty) {

                state.openedSubmenuArr = [name]

            }
        },
    }
};

export default app;